package middle;

public class Solution_81 {

    public boolean search1(int[] nums, int target) {
        int n = nums.length;
        if (n == 0) {
            return false;
        }
        if (n == 1) {
            return nums[0] == target;
        }
        int l = 0, r = n - 1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (nums[mid] == target) {
                return true;
            }
            if (nums[l] == nums[mid] && nums[mid] == nums[r]) {
                ++l;
                --r;
            } else if (nums[l] <= nums[mid]) {
                if (nums[l] <= target && target < nums[mid]) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            } else {
                if (nums[mid] < target && target <= nums[n - 1]) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
        }
        return false;
    }

    public boolean search(int[] nums, int target) {
        return dps(nums, target, 0, nums.length);
    }

    private boolean dps(int[] nums, int target, int begin, int end) {
        if (begin >= end) {
            return false;
        } else if (begin + 1 == end) {
            return nums[begin] == target;
        }
        int mid = (begin + end) / 2;
        if (nums[mid] == target) {
            return true;
        }
        if (nums[mid] == nums[begin] && nums[mid] == nums[end - 1]) {
            return dps(nums, target, begin, mid) || dps(nums, target, mid + 1, end);
        } else if (nums[mid] >= nums[begin] && nums[mid] <= nums[end - 1]) {
            // 升序
            if (nums[mid] > target) {
                return dps(nums, target, begin, mid);
            } else {
                return dps(nums, target, mid + 1, end);
            }
        } else if (nums[mid] >= nums[begin] && nums[mid] >= nums[end - 1]) {
            if (nums[begin] <= target && nums[mid] > target) {
                return dps(nums, target, begin, mid);
            } else {
                return dps(nums, target, mid + 1, end);
            }
        } else {
            if (nums[mid] < target && target <= nums[end - 1]) {
                return dps(nums, target, mid + 1, end);
            } else {
                return dps(nums, target, begin, mid);
            }
        }
    }

    public static void main(String[] args) {
        Solution_81 model = new Solution_81();
        System.out.println(model.search(new int[]{5, 1, 3}, 3));
        System.out.println(model.search(new int[]{0, 0, 1, 1, 2, 0}, 2));
        System.out.println(model.search(new int[]{1, 1, 1, 1, 3, 1}, 3));
        System.out.println(model.search(new int[]{1}, 2));
        System.out.println(model.search(new int[]{1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1}, 2));
        System.out.println(model.search(new int[]{2, 5, 6, 0, 0, 1, 2}, 0));
        System.out.println(model.search(new int[]{2, 5, 6, 0, 0, 1, 2}, 3));

        System.out.println(model.search1(new int[]{3, 4, 5, 6, 7, 1, 2}, 7));
    }
}
